package com.neu.servlet;

import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.neu.dao.UserDao;
import com.neu.daoimpl.UserDaoImpl;
import com.neu.entity.User;

/**
 * Servlet implementation class CookieTimeServlet01
 */
@WebServlet("/CookieTimeServlet01")
public class CookieTimeServlet01 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CookieTimeServlet01() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Servlet#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html; charset=UTF-8");
		request.setCharacterEncoding("utf-8");
		UserDao ud = new UserDaoImpl();
		boolean flag1 = false;
		try {
			flag1 = ud.insert(new User(0, "date"));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//获取所有的Cookie
        Cookie[] cookies = request.getCookies();
        //定义一个标记 用来表示是否存在指定name的Cookie,存在指定cookie,flag为false;不存在为true
        boolean flag = true;

        //如果有Cookie
        if (cookies != null && cookies.length != 0){
            //遍历cookie数组
            for (Cookie cookie : cookies) {
                //判断cookie数组中是否存在指定name的Cookie
                if ("LastTime".equals(cookie.getName())){//如果有指定name的cookie,说明用户非首次访问
                    //更改标记flag的状态值
                    flag = false;
                    //获取指定name cookie对应的值
                    String value = cookie.getValue();
                    //对获取到的值进行解码
                    String timeDecode = URLDecoder.decode(value, "utf-8");
                    //响应页面
                    response.getWriter().write("<h1>欢迎回来,您上次访问的时间是:" + timeDecode + "</h1>");

                    //获取当前时间
                    Date date = new Date();
                    //获取格式化日期对象
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 , HH时mm分ss秒");
                    //格式化当前时间
                    String time = simpleDateFormat.format(date);
                    //对时间字符串进行编码
                    String timeNow = URLEncoder.encode(time, "utf-8");
                    //设置Cookie存活时间
                    cookie.setMaxAge(45);
                    //更改cookie的值
                    cookie.setValue(timeNow);
                    //回写Cookie
                    response.addCookie(cookie);
                    //结束循环
                    break;
                }
            }
            
        }


        //没有Cookie或者Cookie中没有指定name的Cookie,说明用户时首次访问
        if (cookies == null || cookies.length == 0 || flag){
            //获取当前时间
            Date date = new Date();
            //获取格式化日期对象
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 , HH时mm分ss秒");
            //格式化当前时间
            String time = simpleDateFormat.format(date);
            //对时间字符串进行编码
            String timeEncode = URLEncoder.encode(time, "utf-8");
            //将编码后的时间字符串存入Cookie
            Cookie cookie = new Cookie("LastTime", timeEncode);
            //设置Cookie存活时间
            cookie.setMaxAge(45);
            //回写Cookie
            response.addCookie(cookie);
            //响应页面
            response.getWriter().write("<h1>您好,欢迎您首次访问</h1>");

        }

    }
	

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
